<!--APi账户-->
<template>
  <div class="all bg1">
    
    <scroller class="all">
      <div v-if="api">

        <VAssets v-model="eye" :apiId="apiId"></VAssets>

        <div class="address" @click="copyClick(api.address)">
          <text class="font22 text2">{{'充币地址：'}}</text>
          <text class="font22 text2 all text-line1 mr20">{{api.address}}</text>
          <text class="font22 main-t1">{{'点击复制'}}</text>
        </div>

        <VSubMenu class="menu bg1" :type="1" :menu="menu" :menus="menus" @menuClick="menuClick"></VSubMenu>
        
        <div class="line line1"></div>
        <div class="api" v-if="eye">
          <text class="font22 text3">{{account.title}}</text>
          <div class="height10"></div>
          <div class="row aend height50">
            <text class="font38 text2">{{account.totalBtc}}</text>
            <text class="font24 text3 text-up8">{{' ≈'+ account.totalMoney+' '+Config.priceUnit}}</text>
          </div>
        </div>

        <div class="api" v-else>
          <text class="font22 text3">{{account.title}}</text>
          <div class="height10"></div>
          <div class="row aend height50">
            <text class="font38 text2 text-down10">{{'******'}}</text>
            <text class="font24 text3 text-up8">{{' ≈'}}</text>
            <text class="font24 text3 text-up4">{{'******'}}</text>
            <text class="font24 text3 text-up8">{{Config.priceUnit}}</text>
          </div>
        </div>

        <div class="line line1"></div>
        <div class="hide"  @click="hideClick(!hideMin)">
          <LImage class="hide-image" :src="Config.img(hideMin?'api_yc_yes.png':'api_yc_no.png')"></LImage>
          <text class="font24 text3">{{T('隐藏小额币种')}}</text>
        </div>

        <template v-if="menu == 0">
          <VCoinCoin :eye="eye" :item="item" v-for="(item, index) in coins" v-bind:key="'coin'+index"></VCoinCoin>
        </template>
        <template v-if="menu == 1">
          <VCoinUsdt :eye="eye" :item="item" v-for="(item, index) in coins" v-bind:key="'coin'+index"></VCoinUsdt>
        </template>
        <div class="height200"></div>
      </div>
    </scroller>

    <div class="transfer">
      <div class="acenter" @click="tiBiClick">
        <LImage class="transfer-image" :src="Config.img('tibi.png')"></LImage>
        <text class="font24 text2">{{T('提币')}}</text>
      </div>
    </div>
  </div>
</template>

<style src="@/_css/style.css" ></style>
<style scoped>
.menu {
  height: 98px;
  position: sticky;
  margin-bottom: 1px;
}
.line {
  height: 1px;
}

/********************** APi资产*/
.api {
  padding-top: 30px;
  padding-bottom: 30px;
  padding-left: 40px;
  padding-right: 40px;
}
.text-up4 {
  margin-bottom: 4px;
}
.text-up8 {
  margin-bottom: 8px;
}
.text-down10 {
  margin-bottom: -10px;
}

/********************** 显影*/
.hide {
  flex-direction: row;
  align-items: center;
  height: 44px;
  margin-top: 20px;
}
.hide-image {
  width: 24px;
  height: 24px;
  margin-left: 40px;
  margin-right: 20px;
}

/********************** 地址*/
.address {
  overflow: hidden;
  display: flex;
  flex-direction: row;
  padding-top: 30px;
  padding-bottom: 30px;
  padding-left: 40px;
  padding-right: 40px;
}
/********************** 划转*/
.transfer {
  position: absolute;
  flex-direction: column;
  align-items: center;
  height: 200px;

  left: 0;
  right: 0;
  bottom: 0;
}
.transfer-image{
  width: 86px;
  height: 86px;
  margin-top: 20px;
  margin-bottom: 10px;
}

/*宽高*/
.height200 {
  height: 200px;
}
</style>

<script>

//组件
import VAssets from '@/home/view/VAssets.vue'; 
import VSubMenu from '@/_public/VSubMenu.vue'; 
import VCoinCoin from '@/home/view/VCoinCoin.vue'; 
import VCoinUsdt from '@/home/view/VCoinUsdt.vue'; 

//功能
import jnavigator from "@/_js/navigator";
import jconfig from "@/_js/config";
import jmodal from "@/_js/modal";
import jmath from "@/_js/math";
import japi from "@/_js/api";
import jdebug from "@/_js/debug";

const weexFunction = weex.requireModule("WeexFunction");
const globalEvent = weex.requireModule('globalEvent');
const clipboard = weex.requireModule("clipboard");

import I18N from "@/_i18n/i18n";
var T = I18N.t;

export default {

  components: {
    VAssets,
    VSubMenu,
    VCoinCoin,
    VCoinUsdt,
  },

  data() {
    return {
      T:T,
      Config:jconfig,
      eye:true, //Turn显示
      hideMin:true, //true 隐藏小额币种
      
      menu: 0,
      menus:[T('币币账户'), T('USDT合约账户')],

      apiId:undefined,
      api:undefined,
      account:undefined, //账户
      coins:[], //账户下的币
    };
  },

  created() {
    var param = weex.config;
    this.apiId = param.apiId;
    
    jconfig.init(()=>{
      weexFunction.getConfig("hide_min",(value)=>{
        var hideMin = value == "true";
        this.hideMin = hideMin;
        this.initData();
      });
    }, [jconfig.OPT_CURRENT_BZ]);
    
    //Api更新通知
    globalEvent.addEventListener("apiUp",(e)=>{
      this.initData();
    });
  },

  methods: {

    initData() {
      
      /*截取*/
      var guiFan = (assets)=>{
        
        //总资产折合-BTC
        assets.totalBtc = jmath.gfCoin(assets.totalBtc, 'BTC', true);
        
        //总资产折合-钱
        var totalMoney = jconfig.priceUnit == 'USD' ? assets.totalUsd : assets.totalCny;
        assets.totalMoney = jmath.gfMoney(totalMoney, true);
      }
      
      /*截取 币*/
      var jieQvCoin=(coin)=>{

        //单位
        coin.unit = jconfig.priceUnit;

        //数量
        coin.totalNum = jmath.gfCoin(coin.totalNum, coin.currency, true);
        coin.available = jmath.gfCoin(coin.available, coin.currency, true);
        coin.lock = jmath.gfCoin(coin.lock, coin.currency, true);

        //总资产折合-钱
        var totalMoney = jconfig.priceUnit == 'USD' ? coin.totalUsd : coin.totalCny;
        coin.totalMoney = jmath.gfMoney(totalMoney, true);
      }
      
      /*截取 币-合约*/
      var jieQvCoinContract=(coin)=>{

        //账户权益
        coin.marginBalance = jmath.gfCoin(coin.marginBalance, 'USDT', false);
        
        //强平价
        var liquidationPrice = jmath.gfCoin(coin.liquidationPrice, 'BTC', false); 
        coin.liquidationPrice = Number(liquidationPrice) <= 0 ? '--' : liquidationPrice;

        //资产担保率
        var riskRate = jmath.cheng(coin.riskRate, '100', 2, 2);
        coin.riskRate = Number(riskRate) <= 0 ? '--' : (riskRate + '%');
      }

      japi.getApi(this.apiId, 'all', (api)=>{
        weexFunction.setTitle(api.apiName);
        weexFunction.debugLog(api)

        guiFan(api.account); //币币账户
        api.accountAssets = api.accountAssets ? api.accountAssets : [];
        for (let index = 0; index < api.accountAssets.length; index++) {
          var coin = api.accountAssets[index];
          jieQvCoin(coin);
        }

        guiFan(api.linerSwap); //USDT永续账户
        api.linerSwapAssets = api.linerSwapAssets ? api.linerSwapAssets : [];
        for (let index = 0; index < api.linerSwapAssets.length; index++) {
          var coin = api.linerSwapAssets[index];
          jieQvCoinContract(coin);
        }

        api.param.forEach(map => {
          api.address = map.value;
        });

        this.api = api;
        this.menuClick(this.menu);
      });
    },
    
    
    /********************************** 点击触发*/
    //菜单点击
    menuClick(index) {
      this.menu = index;
      this.account = index == 0 ? this.api.account : this.api.linerSwap;
      this.account.title = index == 0 ? T('币币总资产折合(BTC)') : T('USDT合约总资产折合(BTC)');
      this.hideClick(this.hideMin)
    },

    //隐藏小额资产
    hideClick(hideMin) {
      this.hideMin = hideMin;
      var value = hideMin+'';
      weexFunction.setConfig("hide_min", value);

      var datas = this.menu == 0 ? this.api.accountAssets : this.api.linerSwapAssets;
      if (hideMin) {
        var coins = [];
        for (let index = 0; index < datas.length; index++) {
          var coin = datas[index];
          if (Number(coin.totalUsd) > 5) {
            coins.push(coin);
          }
        }
        this.coins = coins;
      } else {
        this.coins = datas;
      }
    },

    //提币
    tiBiClick() {
      
      var param = {'exchangeName': this.api.exchangeName, 'api':this.api};
      jnavigator.open("", "home/BkTx", param);
      return;
    },

    //复制提币地址
    copyClick(address) {
      jmodal.toast(T('复制成功'),2);
      clipboard.setString(address);

    }
  },
};
</script>
